package cn.cuilan.client.procyserver;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

public class ProxyServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        // HTTP请求解码器
        pipeline.addLast("Http-Decoder", new HttpRequestDecoder());
        // HTTP对象聚合器
        pipeline.addLast("http-aggregator", new HttpObjectAggregator(65536));
        // HTTP响应编码器
        pipeline.addLast("http-encoder", new HttpResponseEncoder());
        // HTTP大块数据写入处理器
        pipeline.addLast("http-chunked", new ChunkedWriteHandler());
        // 添加代理服务处理器
        pipeline.addLast(new ProxyServerHandler());
    }
}
